package iSOCK::Controller::Help::Documentation;

use Moose;
use namespace::autoclean;

BEGIN { extends 'iSOCK::Controller::Root' }

=head1 NAME

iSOCK::Controller::Help::Documentation - Documentation Controller for iSOCK

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

sub index :Path :Args(0) {
  my ( $self, $c ) = @_;

  if ( my $session = $c->session ) {
    $self->session_path ( $c );
    
    my $sock_root = $c->config->{directory}->{sock};

    my $chapter;

    my $body;

    if ( open my $fh, '<', "$sock_root/doc/user_guide.html" ) {
      while ( <$fh> ) {
        next unless m/\<body/ .. m/\<\/body/;

        s/\<h1\>/'<h1 id="Chapter-' . ++$chapter . '">'/e;

        s,(href|data)="(?!http://),$1="/SOCK/doc/,g;

        $body .= $_;
      }
    }
    else {
      $body .= "      <p class='error'>Could not open '$sock_root/doc/user_guide.html' file!</p>";
    }

    if ( $chapter ) {
      $c->stash ( alt_navigation => 1, alt_navigation_title => 'Chapters', alt_navigation_items => [ map { { id => "Chapter-$_" } } 1 .. $chapter ] );
    }

    $c->stash ( body => $body );
  }

  $c->forward ( qw( iSOCK::Controller::Root render_view ) );
}

=head1 AUTHOR

Xavier Caron

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
